package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/user/constant"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
)

type RefreshTokenReqBody struct {
	RefreshToken string `json:"refreshToken" binding:"required"` // 学号或者工号
}

var RefreshTokenValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Body: &RefreshTokenReqBody{},
	}
})

type AccessToken struct {
	AccessToken string `json:"accessToken" binding:"required" example:"token"` // 访问令牌
}
type RefreshTokenRespBody struct {
	resp.BaseRespBody
	Data *AccessToken `json:"data" binding:"required"`
}

// RefreshToken
//
//	@Summary				刷新令牌
//	@Description.markdown	RefreshToken
//	@Tags					user
//	@Accept					json
//	@Produce				json
//	@Param					Body	body		RefreshTokenReqBody	true	"JSON 请求体"
//	@Success				200		{object}	RefreshTokenRespBody
//	@Failure				400		{object}	resp.FailedRespBodyString
//	@Router					/v1/users/refresh [post]
func (h *Handler) RefreshToken(c *gin.Context) {
	body := c.MustGet(mw.KeyBody).(*RefreshTokenReqBody)

	if accessToken, err := h.domain.RefreshToken(body.RefreshToken); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeSuccessRefresh, &AccessToken{accessToken})
	}
}
